#include <iostream>
#include <stdexcept>
#include <string>

using namespace std;

class Exception:public out_of_range{
private:
	char *msg;
public:
	Exception(char *msg):out_of_range(msg)
	{
		this->msg = msg;
	}

	Exception(const Exception &e):out_of_range((char *)"copy constructor")
	{
		cout << "拷贝构造函数" << endl;
	}

	~Exception()
	{
		cout << "析构函数" << endl;
	}

	const char * what() const throw()
	{
		return this->msg;
	}
};

void throwException()
{

	Exception e((char *)"custom exception");
	
	// 抛出对象（可以使用对象或者引用在catch中接收）
	throw e;

	// 抛出对象的指针
	//throw &e;

}

int main()
{
#if 0
	try {
		throwException();
	} catch (Exception e) {
		cout << "捕捉到异常" << endl;
		cout << e.what() << endl;
	} catch(...){
		cout << "其他异常" << endl;
	}
#endif

	// 建议使用引用进行接收，可以避免多次拷贝构造函数
	try {
		throwException();
	} catch (const Exception &e) {
		cout << "捕捉到异常" << endl;
		cout << e.what() << endl;
	} catch(...){
		cout << "其他异常" << endl;
	}

#if 0
	try {
		throwException();
	} catch (Exception *e) {
		cout << "捕捉到异常" << endl;
		cout << e->what() << endl;
	} catch(...){
		cout << "其他异常" << endl;
	}
#endif
	return 0;
}




